//
//  ImageHandleViewController.m
//  webApp
//
//  Created by 黄晓容 on 16/5/28.
//  Copyright © 2016年 cloud. All rights reserved.
//

#import "ImageHandleViewController.h"

#define kBoderWidth 1

#pragma mark - 存放触摸手势坐标位置类拓展
@interface ImageHandleViewController()

@property (nonatomic) BOOL canMove; //判断视图是否可以移动
@property (nonatomic) CGPoint beginPoint;

@end

@interface ImageHandleViewController()

@property (nonatomic) CGSize showSize;
@property (nonatomic) CGSize cutSize;

@property (nonatomic,strong) UIImage *image;

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,strong) UIView *cutView;

@property (nonatomic,strong) UIBarButtonItem *rightButton;

@end

@implementation ImageHandleViewController

- (instancetype)initWithImage:(UIImage *)image andCutSize:(CGSize)cutSize
{
    if (self = [super init]) {
        _image = image;
        _cutSize = cutSize;
        self.view.backgroundColor = [UIColor blueColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configParams];
    [self configViews];
}

#pragma mark - 初始化界面
- (void)configViews
{
    self.navigationItem.rightBarButtonItem = self.rightButton;
    [self.view addSubview:self.imageView];
    [self.imageView addSubview:self.cutView];
    self.cutView.center = self.imageView.center;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:self.image];
        _imageView.frame = (CGRect){CGPointZero,self.showSize};
        _imageView.center = self.view.center;
    }
    return _imageView;
}

- (UIView *)cutView
{
    if (!_cutView) {
        CGRect frame = (CGRect){CGPointZero,self.cutSize};
        _cutView = [[UIView alloc] initWithFrame:frame];
        _cutView.layer.borderWidth = kBoderWidth;
        _cutView.layer.borderColor = [[UIColor redColor] CGColor];
        _cutView.backgroundColor = [UIColor clearColor];
    }
    return _cutView;
}

- (UIBarButtonItem *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIBarButtonItem alloc] initWithTitle:@"裁剪" style:UIBarButtonItemStylePlain target:self action:@selector(clickClip)];
    }
    return _rightButton;
}

#pragma mark - 配置参数
- (void)configParams
{
    CGSize imageSize = self.image.size;
    CGSize vcSize = self.view.bounds.size;
    CGFloat phoneRadio = vcSize.width / vcSize.height;
    CGFloat imageRadio = imageSize.width / imageSize.height;
    
    CGSize showSize;
    if (imageRadio > phoneRadio) {
        // 那么图片比例过宽
        showSize.width = vcSize.width;
        showSize.height = showSize.width / imageRadio;
    }else{
        showSize.height = vcSize.height;
        showSize.width = showSize.height * imageRadio;
    }
    self.showSize = showSize;
}

#pragma mark - action
//裁剪图片
-(void)clickClip
{
     CGRect cropFrame = self.cutView.frame;
     CGFloat orgX = cropFrame.origin.x * (self.image.size.width / self.imageView.frame.size.width);
     CGFloat orgY = cropFrame.origin.y * (self.image.size.height / self.imageView.frame.size.height);
     CGFloat width = cropFrame.size.width * (self.image.size.width / self.imageView.frame.size.width);
     CGFloat height = cropFrame.size.height * (self.image.size.height / self.imageView.frame.size.height);
     CGRect cropRect = CGRectMake(orgX, orgY, width, height);
     CGImageRef imgRef = CGImageCreateWithImageInRect(self.image.CGImage, cropRect);
     
     CGFloat deviceScale = [UIScreen mainScreen].scale;
     UIGraphicsBeginImageContextWithOptions(cropFrame.size, 0, deviceScale);
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextTranslateCTM(context, 0, cropFrame.size.height);
     CGContextScaleCTM(context, 1, -1);
     CGContextDrawImage(context, CGRectMake(0, 0, cropFrame.size.width, cropFrame.size.height), imgRef);
     UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
     CGImageRelease(imgRef);
     UIGraphicsEndImageContext();
    
    if([self.imageHandleDelegate respondsToSelector:@selector(imageDidHandle:)]){
        [self.imageHandleDelegate imageDidHandle:newImg];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint beginPoint = [touch locationInView:self.imageView];
    if (CGRectContainsPoint(self.cutView.frame, beginPoint)) {
        self.beginPoint = beginPoint;
        self.canMove = YES;
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.imageView];
    
    CGPoint offsetPoint = CGPointMake(currentPoint.x - self.beginPoint.x, currentPoint.y - self.beginPoint.y);
    
    CGRect frame = self.cutView.frame;
    frame.origin.x += offsetPoint.x;
    frame.origin.y += offsetPoint.y;
    
    if (frame.origin.y <= 0) {
        frame.origin.y = 0;
    }else if(frame.origin.y + self.cutView.frame.size.height >= self.imageView.bounds.size.height){
        frame.origin.y = self.imageView.bounds.size.height - self.cutView.frame.size.height;
    }
    
    if (frame.origin.x <= 0) {
        frame.origin.x = 0;
    }else if(frame.origin.x + self.cutView.frame.size.width >= self.imageView.bounds.size.width){
        frame.origin.x = self.imageView.bounds.size.width - self.cutView.frame.size.width;
    }
    
    if (self.canMove) {
        self.cutView.frame = frame;
        self.beginPoint = currentPoint;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.canMove = NO;
}

@end
